package com.itheima.chapter09.service;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;

@Service
public class ScheduledTaskService {
    private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private Integer count1 = 1;
    private Integer count2 = 1;
    private Integer count3 = 1;

    @Scheduled(fixedRate = 60000)
    public void scheduledTaskImmediately() {
        System.out.println(String.format("fixedRate 第 %s 次执行，当前时间为：%s", count1++, sdf.format(System.currentTimeMillis())));
    }

    @Scheduled(fixedDelay = 60000)
    public void scheduledTaskAfterSleep() throws InterruptedException {
        System.out.println(String.format("fixedDelay 第 %s 次执行，当前时间为：%s", count2++, sdf.format(System.currentTimeMillis())));
        Thread.sleep(10000);
    }

    @Scheduled(cron = "0 * * * * *")
    public void scheduledTaskCron() {
        System.out.println(String.format("cron 第 %s 次执行，当前时间为：%s", count3++, sdf.format(System.currentTimeMillis())));
    }
}
